package cn.gok.controller;


import cn.gok.entity.Course;
import cn.gok.entity.Student;
import cn.gok.service.CourseService;
import cn.gok.service.StudentService;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author: LKSV
 * @Description:
 * @Date:Created in 19:29 2021/7/27
 */
@Controller
public class StudentController {
    @Resource
    private StudentService studentService;


    @GetMapping("/student/login")
    public ModelAndView loginStudent(String name,String password,ModelAndView modelAndView){
        Student s = studentService.selectByName(name,password);
        modelAndView.addObject("student",s);
        modelAndView.setViewName("t_student");
        if (s==null){
            return null;
        }else {
            return modelAndView;
        }
    }

    @GetMapping("/student/selectClass")
    @ResponseBody
    public List<Course> selectStudentClass(Integer id){

//        PageHelper.startPage(1, 3, true);
//        List<Course> courses = this.studentService.selectByClass(id);
//
//        for (Course course : courses) {
//            System.out.println(course);

        return studentService.selectByClass(id);
    }

    @PostMapping("/student/register")
    public String insert(Student record, HttpServletRequest request, Model model,BindingResult errorResult){
        record.setStudentName(request.getParameter("student_name"));
        record.setPassword(request.getParameter("password"));
        record.setRealName(request.getParameter("real_name"));
        record.setAge( request.getIntHeader("age"));
        record.setGender(request.getParameter("gender"));

        //数据有问题，进行处理
        if(errorResult.hasErrors()){
            List<FieldError> fieldErrors=errorResult.getFieldErrors();

            for(FieldError fieldError:fieldErrors){
                //获得错误字段
                String fieldname=fieldError.getField();
                // 获得错误提示信息
                String errorMessage=fieldError.getDefaultMessage();

                // 将的到错误字段和信息封装在model
                model.addAttribute(fieldname,errorMessage);

                // 将用户信息回显给前台，通过el表达式取出，提升用户体验
                model.addAttribute("student",record);
            }
            return "views/register";

        }else{
            // 数据没问题，可以通过
            System.out.println(record);
            studentService.insert(record);
            return "index";
        }
    }

    @GetMapping("/student/findClass")
    @ResponseBody
    public List<Course> studentClass(){
        return studentService.selectStudentClass();
    }
}
